/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年8月29日
 */
package com.massyframework.beanskin.assembly.handling;

import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;

import com.massyframework.beanskin.assembly.Assembly;

/**
 * {@link HandlerBase}，提供{@link Handler}常用方法的封装.
 * @author  Huangkaihui
 *
 */
public abstract class HandlerBase implements Handler {

	private LifecycleManager lifecycleManager;
	
	/**
	 * 创建一个新的{@link HandlerBase}
	 */
	public HandlerBase() {
	}

	@Override
	public final void init(LifecycleManager lifecycleManager) throws Exception {
		this.lifecycleManager = lifecycleManager;
        this.init();;
	}
	
	/**
	 * 初始化
	 * @throws Exception 初始化期间发生非预期例外
	 */
	protected void init() throws Exception{
		
	}
	
	/**
	 * 判断是否已经初始化
	 * @return {@link boolean}, 返回{@code true}表示已经初始化，否则返回{@code false}
	 */
	protected boolean inited() {
		return this.lifecycleManager != null;
	}
	
	/**
     * 生命周期管理器
     * @return {@link HandlerRegistry}
     */
    protected LifecycleManager getLifecycleManager(){
    	Objects.requireNonNull(this.lifecycleManager, "\"lifecycleManager\" cannot be null, please init it:" + this.getClass().getName());
        return this.lifecycleManager;
    }

    /**
     * 关联的装配件
     * @return {@link ServiceExportableModule}
     */
    public Assembly getAssociatedAssembly(){
        LifecycleManager lifecycleManager = this.getLifecycleManager();
        return lifecycleManager == null ?
                null :
                lifecycleManager.getAssociatedAssembly();
    }

    /**
     * 根据查找任意派生于<code>handlerType</code>的处理器
     * @param handlerType {@link Class}, 处理器类型
     * @param <H>
     * @return {@link H}, 可能返回null.
     * @throws HandlerNotInitedException 处理器未执行初始化方法抛出的例外
     */
    protected <H> H findHandler(Class<H> handlerType) throws HandlerNotInitedException {
        LifecycleManager lifecycleManager= this.getLifecycleManager();
        if (lifecycleManager == null) {
            throw new HandlerNotInitedException(this);
        }

        return lifecycleManager.findHandler(handlerType);
    }

    /**
     * 根据查找任意派生于<code>handlerType</code>的处理器
     * @param handlerType {@link Class}, 处理器类型
     * @param <H>
     * @return {@link H}
     * @throws HandlerNotInitedException 处理器未执行初始化方法抛出的例外
     * @throws HandlerNotFoundException 未找到合适处理器抛出的例外
     */
    protected <H> H getHandler(Class<H> handlerType)
            throws HandlerNotInitedException, HandlerNotFoundException{
        LifecycleManager lifecycleManager= this.getLifecycleManager();
        if (lifecycleManager == null) {
            throw new HandlerNotInitedException(this);
        }

        return lifecycleManager.getHandler(handlerType);
    }

    /**
     * 获取所有派生<code>handlerType</code>的处理器
     * @param handlerType {@link Class}, 处理器类型
     * @param <H>
     * @return {@link List}
     * @throws HandlerNotInitedException 处理器未执行初始化方法抛出的例外
     */
    protected <H> List<H> getHandlers(Class<H> handlerType) throws HandlerNotInitedException {
        LifecycleManager lifecycleManager= this.getLifecycleManager();
        if (lifecycleManager == null) {
            throw new HandlerNotInitedException(this);
        }

        return lifecycleManager.getHandlers(handlerType);
    }

    /**
     * 记录调试消息
     * @param message 消息
     */
    public void logDebug(String message) {
        Logger logger = this.getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(message);
        }
    }

    /**
     * 记录调试消息
     * @param message 消息
     * @param cause 例外
     */
    public void logDebug(String message, Throwable cause) {
        Logger logger = this.getLogger();
        if (logger.isDebugEnabled()) {
            logger.debug(message, cause);
        }
    }

    /**
     * 记录错误消息
     * @param message 消息
     */
    public void logError(String message) {
        Logger logger = this.getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(message);
        }
    }

    /**
     * 记录错误消息
     * @param message 消息
     * @param cause 例外
     */
    public void logError(String message, Throwable cause) {
        Logger logger = this.getLogger();
        if (logger.isErrorEnabled()) {
            logger.error(message, cause);
        }
    }

    /**
     * 记录消息
     * @param message 消息
     */
    public void logInfo(String message) {
        Logger logger = this.getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(message);
        }
    }

    /**
     * 记录消息
     * @param message 消息
     * @param cause 例外
     */
    public void logInfo(String message, Throwable cause) {
        Logger logger = this.getLogger();
        if (logger.isInfoEnabled()) {
            logger.info(message, cause);
        }
    }

    /**
     * 记录跟踪消息
     * @param message 消息
     */
    public void logTrace(String message) {
        Logger logger = this.getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(message);
        }
    }

    /**
     * 记录跟踪消息
     * @param message 消息
     * @param cause 例外
     */
    public void logTrace(String message, Throwable cause) {
        Logger logger = this.getLogger();
        if (logger.isTraceEnabled()) {
            logger.trace(message, cause);
        }
    }

    /**
     * 记录警告消息
     * @param message 消息
     */
    public void logWarn(String message) {
        Logger logger = this.getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(message);
        }
    }

    /**
     * 记录警告消息
     * @param message 消息
     * @param cause 例外
     */
    public void logWarn(String message, Throwable cause) {
        Logger logger = this.getLogger();
        if (logger.isWarnEnabled()) {
            logger.warn(message, cause);
        }
    }

    /**
     * 日志记录器
     * @return {@link Logger}
     */
    public Logger getLogger() {
        LifecycleManager lifecycleManager = this.getLifecycleManager();
        if (lifecycleManager == null){
            throw new HandlerNotInitedException(this);
        }
        return lifecycleManager.getAssociatedAssembly().getLogger();
    }

	@Override
	public void destroy() {		
	}


}
